package com.itheima.controller;


import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Menu;
import com.itheima.service.MenuService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/menu")
public class MenuController {
    @Reference
    private MenuService menuService;

    @RequestMapping("/findAll")
    public Result findAll(){
        try {
            List<Menu> list = menuService.findAll();
            return new Result(true,"查询菜单成功",list);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"查询菜单失败");
        }
    }
    //菜单添加
    @RequestMapping("/add")
    public Result add(@RequestBody Menu menu){
        try {
            menuService.add(menu);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "添加菜单失败");
        }
        return new Result(true,"添加菜单成功");
    }
    //分页查询
    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){
        PageResult pageResult = menuService.pageQuery(queryPageBean);
        return pageResult;
    }

    //根据id查询
    @RequestMapping("/findById")
    public Result findById(Integer id){

        try {
            Menu menu= menuService.findById(id);
            return new Result(true,"查询成功",menu);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return null;
    }

    //编辑菜单
    @RequestMapping("/edit")
    public Result edit(@RequestBody Menu menu){

        try {
            menuService.edit(menu);
            return new Result(true,"菜单编辑成功");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new Result(false,"菜单编辑失败");
    }

    @RequestMapping("/delete")
    public Result delete(Integer id){

        try {
            menuService.deleteById(id);
            return new Result(true,"菜单删除成功");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new Result(false,"菜单删除失败");
    }

}
